﻿using Fast.AspNetCoreMvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;

namespace Fast.Validation
{
    /// <summary>
    /// 模型验证过滤器
    /// </summary>
    [SkipScan]
    public class FastValidationFilter : IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            if (!context.ActionDescriptor.IsControllerAction())
            {
                await next();
                return;
            }

            context.GetRequiredService<IModelStateValidator>().Validate(context.ModelState);
            await next();
        }
    }
}
